package com.bee.base.http;

import com.bee.base.http.model.RequestEntry;
import com.bee.base.string.StringUtils;
import lombok.Data;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ByteArrayEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.util.Map;

/**
 * http远程调用工具类
 * @description: http远程调用工具类
 * @author: chenguosong
 * @date: 2022-11-15 14:14
 */
@Data
public class HttpUtils {

    private static String token = "";

    /**
     * 连接超时时间
     */
    private static int socketTimeout =60000;

    /**
     * 建立连接超时的时间
     */
    private static int connectTimeout=3000;

    /**
     * 请求超时的时间
     */
    private static int requestTimeout=3000;

    /**
     * http执行post请求
     * @param request
     * @return 调用服务返回的信息
     */
    public static String post(RequestEntry request) {
        RequestConfig defaultRequestConfig = RequestConfig.custom()
                //请求获取数据的超时时间，单位毫秒。 如果访问一个接口，多少时间内无法返回数据，就直接放弃此次调用。
                .setSocketTimeout(socketTimeout)
                //设置连接超时时间，单位毫秒
                .setConnectTimeout(connectTimeout)
                //设置从connect Manager获取Connection 超时时间，单位毫秒
                .setConnectionRequestTimeout(requestTimeout)
                .build();
        HttpClientBuilder httpClientBuilder = HttpClientBuilder.create().setDefaultRequestConfig(defaultRequestConfig);
        String requestURI = request.getRequestURI();

        int flag = 0;
        // 设置请求链接的参数
        if (null != request.getParams() && !request.getParams().isEmpty()) {
            for (Map.Entry<String, String> entry : request.getParams().entrySet()) {
                if(flag!=0){
                    requestURI+= "&" +entry.getKey()+"="+entry.getValue();
                }else{
                    requestURI+= "?" +entry.getKey()+"="+entry.getValue();
                    flag++;
                }

            }
        }
        // 创建httpPost远程连接实例
        HttpPost httpPost = new HttpPost(requestURI);
        String result = "";
        try (CloseableHttpClient closeableHttpClient = httpClientBuilder.build()) {
            // 设置请求头参数
            if (null != request.getHeaders() && !request.getHeaders().isEmpty()) {
                for (Map.Entry<String, String> entry : request.getHeaders().entrySet()) {
                    httpPost.setHeader(entry.getKey(), entry.getValue());
                }
            }
            // 设置请求体参数
            if (!StringUtils.isEmpty(request.getRequestBody())) {
                HttpEntity requestEntity = new ByteArrayEntity(request.getRequestBody()
                        .getBytes("UTF-8"));
                httpPost.setEntity(requestEntity);
            }

            // 执行之后返回的内容
            HttpResponse resp = closeableHttpClient.execute(httpPost);
            HttpEntity inEntity = resp.getEntity();
            result = EntityUtils.toString(inEntity, "UTF-8");
        } catch (IOException e) {
            e.printStackTrace();
        }
        return result;
    }
}
